home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / imghdr.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  5KB  |  164 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Recognize image file formats based on their first few bytes.'''
  5. __all__ = [
  6.     'what']
  7.  
  8. def what(file, h = None):
  9.     if h is None:
  10.         if isinstance(file, basestring):
  11.             f = open(file, 'rb')
  12.             h = f.read(32)
  13.         else:
  14.             location = file.tell()
  15.             h = file.read(32)
  16.             file.seek(location)
  17.             f = None
  18.     else:
  19.         f = None
  20.     
  21.     try:
  22.         for tf in tests:
  23.             res = tf(h, f)
  24.             if res:
  25.                 return res
  26.     finally:
  27.         if f:
  28.             f.close()
  29.         
  30.  
  31.  
  32. tests = []
  33.  
  34. def test_jpeg(h, f):
  35.     '''JPEG data in JFIF format'''
  36.     if h[6:10] == 'JFIF':
  37.         return 'jpeg'
  38.  
  39. tests.append(test_jpeg)
  40.  
  41. def test_exif(h, f):
  42.     '''JPEG data in Exif format'''
  43.     if h[6:10] == 'Exif':
  44.         return 'jpeg'
  45.  
  46. tests.append(test_exif)
  47.  
  48. def test_png(h, f):
  49.     if h[:8] == '\x89PNG\r\n\x1a\n':
  50.         return 'png'
  51.  
  52. tests.append(test_png)
  53.  
  54. def test_gif(h, f):
  55.     """GIF ('87 and '89 variants)"""
  56.     if h[:6] in ('GIF87a', 'GIF89a'):
  57.         return 'gif'
  58.  
  59. tests.append(test_gif)
  60.  
  61. def test_tiff(h, f):
  62.     '''TIFF (can be in Motorola or Intel byte order)'''
  63.     if h[:2] in ('MM', 'II'):
  64.         return 'tiff'
  65.  
  66. tests.append(test_tiff)
  67.  
  68. def test_rgb(h, f):
  69.     '''SGI image library'''
  70.     if h[:2] == '\x01\xda':
  71.         return 'rgb'
  72.  
  73. tests.append(test_rgb)
  74.  
  75. def test_pbm(h, f):
  76.     '''PBM (portable bitmap)'''
  77.     if len(h) >= 3 and h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
  78.         return 'pbm'
  79.  
  80. tests.append(test_pbm)
  81.  
  82. def test_pgm(h, f):
  83.     '''PGM (portable graymap)'''
  84.     if len(h) >= 3 and h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
  85.         return 'pgm'
  86.  
  87. tests.append(test_pgm)
  88.  
  89. def test_ppm(h, f):
  90.     '''PPM (portable pixmap)'''
  91.     if len(h) >= 3 and h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
  92.         return 'ppm'
  93.  
  94. tests.append(test_ppm)
  95.  
  96. def test_rast(h, f):
  97.     '''Sun raster file'''
  98.     if h[:4] == 'Y\xa6j\x95':
  99.         return 'rast'
  100.  
  101. tests.append(test_rast)
  102.  
  103. def test_xbm(h, f):
  104.     '''X bitmap (X10 or X11)'''
  105.     s = '#define '
  106.     if h[:len(s)] == s:
  107.         return 'xbm'
  108.  
  109. tests.append(test_xbm)
  110.  
  111. def test_bmp(h, f):
  112.     if h[:2] == 'BM':
  113.         return 'bmp'
  114.  
  115. tests.append(test_bmp)
  116.  
  117. def test():
  118.     import sys as sys
  119.     recursive = 0
  120.     if sys.argv[1:] and sys.argv[1] == '-r':
  121.         del sys.argv[1:2]
  122.         recursive = 1
  123.     
  124.     
  125.     try:
  126.         if sys.argv[1:]:
  127.             testall(sys.argv[1:], recursive, 1)
  128.         else:
  129.             testall([
  130.                 '.'], recursive, 1)
  131.     except KeyboardInterrupt:
  132.         sys.stderr.write('\n[Interrupted]\n')
  133.         sys.exit(1)
  134.  
  135.  
  136.  
  137. def testall(list, recursive, toplevel):
  138.     import sys
  139.     import os as os
  140.     for filename in list:
  141.         if os.path.isdir(filename):
  142.             print filename + '/:',
  143.             if recursive or toplevel:
  144.                 print 'recursing down:'
  145.                 import glob as glob
  146.                 names = glob.glob(os.path.join(filename, '*'))
  147.                 testall(names, recursive, 0)
  148.             else:
  149.                 print '*** directory (use -r) ***'
  150.         toplevel
  151.         print filename + ':',
  152.         sys.stdout.flush()
  153.         
  154.         try:
  155.             print what(filename)
  156.         continue
  157.         except IOError:
  158.             print '*** not found ***'
  159.             continue
  160.         
  161.  
  162.     
  163.  
  164.